#include <iostream>

//c
#include <unistd.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>

int main() {
    int socketpipe[2];
    if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketpipe) == -1)
        return -1;

    {// 类似管道, 一头写, 一头读    
        char buf[128] = {0};
        write(socketpipe[0], "hello pipe[0]!", 15);
        read(socketpipe[1], buf, 128);
        std::cout << "read from pipe[1]: " << buf << std::endl;

        write(socketpipe[1], "hello pipe[1]!", 15);
        read(socketpipe[0], buf, 128);
        std::cout << "read from pipe[0]: " << buf << std::endl;
    }

    {//多进程读写
        int pid = fork();

        if (pid == 0) { //child
            std::cout << "child process" << std::endl;
            close(socketpipe[0]);
            write(socketpipe[1], "i am child, write to pipe[0]!", 30);
            exit(0);
        }

        else {
            std::cout << "father process" << std::endl;
            close(socketpipe[1]);
            char buf[128] = {0};
            read(socketpipe[0], buf, 30);

            std::cout << "i am father, read from pipe[0]: " << buf << std::endl;
        }
    }
}